#include "user_i2c.h"


/********************静态变量定义********************/
static int8_t i2c_status[I2C_NUM] = {[0 ... I2C_NUM-1]=I2C_OK};
static uint8_t rx_buffer[I2C_BUFFER_SIZE] = {0}, tx_buffer[I2C_BUFFER_SIZE] = {0};//I2C收发设置1
static uint16_t tx_transfer_count = 1, rx_transfer_count = 2,i2c_dma_serial_num=0;//I2C收发设置2
static bool is_i2c_dma_enabled=false;

/********************静态函数声明********************/
static bool i2c_wait_controller_status(I2C_Regs *this_i2c,uint32_t i2c_status_assert,bool invert, uint16_t tick_to_wait);
static uint8_t which_i2c_inst(I2C_Regs *this_i2c);

/********************全局函数定义********************/
void i2c_dma_init()
{
    /*DMA部分配置*/
    // DL_I2C_enableDMAEvent(I2C0, DL_I2C_EVENT_ROUTE_1,
    //                     DL_I2C_DMA_INTERRUPT_CONTROLLER_RXFIFO_TRIGGER);
    // DL_I2C_enableDMAEvent(I2C1, DL_I2C_EVENT_ROUTE_1,
    //                     DL_I2C_DMA_INTERRUPT_CONTROLLER_RXFIFO_TRIGGER);
    DL_DMA_setTrigger(DMA,DMA_I2C_READ_CHAN_ID,DMA_SOFTWARE_TRIG,
            DL_DMA_TRIGGER_TYPE_EXTERNAL);
    DL_DMA_setDestAddr(DMA,DMA_I2C_READ_CHAN_ID,(uint32_t)&rx_buffer[0]);

    /*将DMA源转到I2C0-RX*/
    i2c_dma_serial_num=0;
    DL_DMA_setSrcAddr(DMA,DMA_I2C_READ_CHAN_ID,(uint32_t)&I2C0->MASTER.MRXDATA);

    /*使能I2C-DMA标志位*/
    is_i2c_dma_enabled=true;
}
/**
 * @brief i2c写入
 * @param this_i2c i2c句柄
 * @param slave_addr 从机地址
 * @param reg_addr 寄存器地址，输入的值大于0xFF即表示直接发送数据
 * @param data 写入数据指针
 * @param count 写入数据数
 * @return i2c写入后状态
 **/
int8_t i2c_write(I2C_Regs *this_i2c,uint8_t slave_addr,uint16_t reg_addr,uint8_t* data,uint16_t count)
{
    uint8_t this_i2c_serial_num=I2C_NUM;
    int8_t  this_i2c_status=I2C_OK;

    /*检查I2C端口正确性*/
    this_i2c_serial_num=which_i2c_inst(this_i2c);
    if(this_i2c_serial_num>=I2C_NUM)
    {
        return I2C_SERIAL_NUMBER_OUT_OF_RANGE;
    }
    this_i2c_status=i2c_status[this_i2c_serial_num];

    /*检查I2C状态，复位i2c_status*/
    if(this_i2c_status == I2C_ERROR||this_i2c_status == I2C_FAILED|| (DL_I2C_getControllerStatus(this_i2c) & DL_I2C_CONTROLLER_STATUS_ERROR))
    {
        DL_I2C_resetControllerTransfer(this_i2c);//重置i2c通信

        // if(DL_I2C_getControllerStatus(this_i2c) & DL_I2C_CONTROLLER_STATUS_ERROR)
        // {
        //     i2c_status[this_i2c_serial_num] = this_i2c_status = I2C_ERROR;
        //     return i2c_status[this_i2c_serial_num];
        // }    
    }
    this_i2c_status = I2C_OK;

    /*检查当前I2c方向，如果是接收则转换方向并提供转换时间*/
    if(DL_I2C_getControllerDirection(this_i2c)==DL_I2C_CONTROLLER_DIRECTION_RX)
    {
        DL_I2C_setControllerDirection(this_i2c,DL_I2C_CONTROLLER_DIRECTION_TX);
        delay_ms(20);
    }

    /*检查发送数量是否正确*/
    if(count==0||count>I2C_BUFFER_SIZE)
    {
        i2c_status[this_i2c_serial_num] = this_i2c_status = I2C_DATA_COUNT_ERROR;
        return i2c_status[this_i2c_serial_num];
    }

    /*设置I2C发送内容大小和信息*/
    //寄存器方式写入
    if(reg_addr<=0x00FF)
    {
        tx_transfer_count=count+1;
        tx_buffer[0]=(uint8_t)reg_addr;
        memcpy(tx_buffer+1,data,tx_transfer_count-1);
    }
    //直接写入
    else
    {
        tx_transfer_count=count;
        memcpy(tx_buffer,data,tx_transfer_count);
    }

    DL_I2C_flushControllerTXFIFO(this_i2c);//清空TXFIFO
    DL_I2C_fillControllerTXFIFO(this_i2c,tx_buffer,tx_transfer_count>8?8:tx_transfer_count);//TXFIFO填充发送内容

    /*等待I2C空闲，超时退出*/
    if(i2c_wait_controller_status(this_i2c,DL_I2C_CONTROLLER_STATUS_IDLE,false,200)==false)
    {
        i2c_status[this_i2c_serial_num] = this_i2c_status =I2C_BUSY;
        return i2c_status[this_i2c_serial_num];
    }
    
    /*分组发送*/
    for(uint16_t i=(tx_transfer_count-1)/8+1;i>0;i--)
    {
        /*如果写入第一组数据,且不止一组数据*/
        if(i==(tx_transfer_count-1)/8+1&&!((tx_transfer_count-1)/8+1==1))
        {
            DL_I2C_startControllerTransferAdvanced(this_i2c, slave_addr,DL_I2C_CONTROLLER_DIRECTION_TX,
                8,
                DL_I2C_CONTROLLER_START_ENABLE,
                DL_I2C_CONTROLLER_STOP_DISABLE,
                DL_I2C_CONTROLLER_ACK_ENABLE
            );
        }
        /*受MSPM0 FIFO限制，写入超过8以上的字节时，每8个字节需要再发起一次写入,隐含条件：要写入的数据不止一组,且不为第一组数据*/
        else if(i!=1)
        {
            DL_I2C_flushControllerTXFIFO(this_i2c);//清空TXFIFO
            DL_I2C_fillControllerTXFIFO(this_i2c,tx_buffer+(((tx_transfer_count-1)/8+1-i)*8),8);//TXFIFO填充发送内容
            DL_I2C_startControllerTransferAdvanced(this_i2c, slave_addr,DL_I2C_CONTROLLER_DIRECTION_TX,
                8,
                DL_I2C_CONTROLLER_START_DISABLE,
                DL_I2C_CONTROLLER_STOP_DISABLE,
                DL_I2C_CONTROLLER_ACK_ENABLE
            );
        }
        /*如果写入最后一组数据,且不止一组数据*/
        else if(i==1&&!((tx_transfer_count-1)/8+1==1))
        {
            DL_I2C_flushControllerTXFIFO(this_i2c);//清空TXFIFO
            volatile uint8_t test=tx_transfer_count%8==0?8:tx_transfer_count%8;
            DL_I2C_fillControllerTXFIFO(this_i2c,tx_buffer+(((tx_transfer_count-1)/8)*8),test);//TXFIFO填充发送内容
            DL_I2C_startControllerTransferAdvanced(this_i2c, slave_addr,DL_I2C_CONTROLLER_DIRECTION_TX,
                test,
                DL_I2C_CONTROLLER_START_DISABLE,
                DL_I2C_CONTROLLER_STOP_ENABLE,
                DL_I2C_CONTROLLER_ACK_ENABLE
            );
        }
        /*如果只有一组数据*/
        else if((tx_transfer_count-1)/8+1==1)
        {
            DL_I2C_startControllerTransferAdvanced(this_i2c, slave_addr,DL_I2C_CONTROLLER_DIRECTION_TX,tx_transfer_count,
                DL_I2C_CONTROLLER_START_ENABLE,
                DL_I2C_CONTROLLER_STOP_ENABLE,
                DL_I2C_CONTROLLER_ACK_ENABLE
            );      
        }

        /*等待该组数据发送完毕*/
        i2c_wait_controller_status(this_i2c,I2C_TXFIFO_IS_EMPTY,false,UINT16_MAX);
    }

    /*等待I2C通信完毕。如果等待超时，重置i2c通信*/
    if(i2c_wait_controller_status(this_i2c,DL_I2C_CONTROLLER_STATUS_ERROR,false,200)==true)
    {
        DL_I2C_resetControllerTransfer(this_i2c);//重置i2c通信
        this_i2c_status=I2C_ERROR;
    }
    /*等待I2C和I2C总线空闲*/
    i2c_wait_controller_status(this_i2c,DL_I2C_CONTROLLER_STATUS_IDLE,false,UINT16_MAX);
    i2c_wait_controller_status(this_i2c,DL_I2C_CONTROLLER_STATUS_BUSY_BUS,true,UINT16_MAX);
    //delay_ms(50);

    /*返回i2c状态*/
    i2c_status[this_i2c_serial_num]=this_i2c_status;
    return i2c_status[this_i2c_serial_num];
}


/**
 * @brief i2c读取
 * @param this_i2c i2c句柄
 * @param slave_addr 从机地址
 * @param reg_addr 寄存器地址,输入的值大于0xFF即表示不发起写入寄存器地址的操作，直接读取数据
 * @param data 读取数据指针
 * @param count 读取数据数
 * @return i2c读取后状态
 **/
int8_t i2c_read(I2C_Regs *this_i2c,uint8_t slave_addr,uint16_t reg_addr,uint8_t* data,uint16_t count)
{
    uint8_t this_i2c_serial_num=I2C_NUM;
    int8_t  this_i2c_status=I2C_OK;
    uint16_t received_count=0;

    /*检查I2C端口正确性*/
    this_i2c_serial_num=which_i2c_inst(this_i2c);
    if(this_i2c_serial_num>=I2C_NUM)
    {
        return I2C_SERIAL_NUMBER_OUT_OF_RANGE;
    }
    this_i2c_status=i2c_status[this_i2c_serial_num];

    /*检查I2C状态，复位i2c_status*/
    if(this_i2c_status == I2C_ERROR||this_i2c_status == I2C_FAILED|| (DL_I2C_getControllerStatus(this_i2c) &
    DL_I2C_CONTROLLER_STATUS_ERROR))
    {
        DL_I2C_resetControllerTransfer(this_i2c);//重置i2c通信

        /*测试用，实际使用屏蔽*/
        // DL_I2C_disablePower(this_i2c);
        // DL_I2C_reset(this_i2c);
        // DL_I2C_enablePower(this_i2c);
        
        // if(this_i2c_serial_num==0)
        // {
        //     SYSCFG_DL_GRAY_I2C_init();
        // }   
        // else
        // {
        //     SYSCFG_DL_IMU_I2C_init();
        // }
        // if(DL_I2C_getControllerStatus(this_i2c) & DL_I2C_CONTROLLER_STATUS_ERROR)
        // {
        //     i2c_status[this_i2c_serial_num] = this_i2c_status = I2C_ERROR;
        //     return i2c_status[this_i2c_serial_num];
        // }
        /**/
    }
    this_i2c_status = I2C_OK;

    /*检查当前I2c方向，如果是接收则转换方向并提供转换时间*/
    if(DL_I2C_getControllerDirection(this_i2c)==DL_I2C_CONTROLLER_DIRECTION_TX)
    {
        DL_I2C_setControllerDirection(this_i2c,DL_I2C_CONTROLLER_DIRECTION_RX);
        delay_ms(20);
    }

    /*检查发送数量是否正确*/
    if(count==0||count>I2C_BUFFER_SIZE)
    {
        i2c_status[this_i2c_serial_num] = this_i2c_status = I2C_DATA_COUNT_ERROR;
        return i2c_status[this_i2c_serial_num];
    }

    /*设置I2C发送内容大小和信息以及接收内容大小，清除接收缓存*/
    tx_transfer_count=1,rx_transfer_count=count;
    tx_buffer[0]=(uint8_t)reg_addr;
    memset(rx_buffer,0,sizeof(rx_buffer));

    /*等待I2C空闲，超时退出*/
    if(i2c_wait_controller_status(this_i2c,DL_I2C_CONTROLLER_STATUS_IDLE,false,200)==false)
    {
        i2c_status[this_i2c_serial_num] = this_i2c_status =I2C_BUSY;
        return i2c_status[this_i2c_serial_num];
    }
    /*配置DMA*/
    if(is_i2c_dma_enabled==true)
    {
        DL_DMA_disableChannel(DMA,DMA_I2C_READ_CHAN_ID);

        if(i2c_dma_serial_num!=this_i2c_serial_num)
        {
            i2c_dma_serial_num=this_i2c_serial_num;
            switch (this_i2c_serial_num)
            {
            case 0 :
                /* code */
                // DL_DMA_setTrigger(DMA,DMA_I2C_READ_CHAN_ID,DMA_I2C0_RX_TRIG,
                //         DL_DMA_TRIGGER_TYPE_EXTERNAL);
                
                DL_DMA_setSrcAddr(DMA,DMA_I2C_READ_CHAN_ID,(uint32_t)&I2C0->MASTER.MRXDATA);
                break;
            case 1 :
                /* code */
                // DL_DMA_setTrigger(DMA,DMA_I2C_READ_CHAN_ID,DMA_I2C1_RX_TRIG,
                //         DL_DMA_TRIGGER_TYPE_EXTERNAL);
                DL_DMA_setSrcAddr(DMA,DMA_I2C_READ_CHAN_ID,(uint32_t)&I2C1->MASTER.MRXDATA);
                break;        
            default:
                break;
            }        
        }
        //DL_DMA_setTransferSize(DMA,DMA_I2C_READ_CHAN_ID,rx_transfer_count);

        DL_DMA_enableChannel(DMA,DMA_I2C_READ_CHAN_ID);
    }
    /*寄存器方式读取数据*/
    if(reg_addr<=0x00FF)
    {
        DL_I2C_flushControllerTXFIFO(this_i2c);//清空TXFIFO

        DL_I2C_fillControllerTXFIFO(this_i2c,tx_buffer,tx_transfer_count);//TXFIFO填充发送内容

        /*I2C发送要读取的寄存器*/
        DL_I2C_startControllerTransferAdvanced(this_i2c, slave_addr,DL_I2C_CONTROLLER_DIRECTION_TX,tx_transfer_count,
            DL_I2C_CONTROLLER_START_ENABLE,
            DL_I2C_CONTROLLER_STOP_DISABLE,
            DL_I2C_CONTROLLER_ACK_ENABLE
        );
        delay_ms(20);
    }

    /*分组接收*/
    for(uint16_t i=(rx_transfer_count-1)/8+1;i>0;i--)
    {
        uint8_t rx_buffer_group_size_verify=8,rx_buffer_group_size=0;
        /*如果读取第一组数据,且不止一组数据*/
        if(i==(rx_transfer_count-1)/8+1&&!((rx_transfer_count-1)/8+1==1))
        {
            DL_I2C_startControllerTransferAdvanced(this_i2c, slave_addr,DL_I2C_CONTROLLER_DIRECTION_RX,
                8,
                DL_I2C_CONTROLLER_START_ENABLE,
                DL_I2C_CONTROLLER_STOP_DISABLE,
                DL_I2C_CONTROLLER_ACK_ENABLE
            );
        }
        /*受MSPM0 FIFO限制，读取超过8以上的字节时，每8个字节需要再发起一次读取,隐含条件：要读取的数据不止一组,且不为第一组数据*/
        else if(i!=1)
        {
            DL_I2C_startControllerTransferAdvanced(this_i2c, slave_addr,DL_I2C_CONTROLLER_DIRECTION_RX,
                8,
                DL_I2C_CONTROLLER_START_DISABLE,
                DL_I2C_CONTROLLER_STOP_DISABLE,
                DL_I2C_CONTROLLER_ACK_ENABLE
            );
        }
        /*如果接收最后一组数据,且不止一组数据*/
        else if(i==1&&!((rx_transfer_count-1)/8+1==1))
        {
            rx_buffer_group_size_verify=(rx_transfer_count%8==0?8:rx_transfer_count%8);
            DL_I2C_startControllerTransferAdvanced(this_i2c, slave_addr,DL_I2C_CONTROLLER_DIRECTION_RX,
                rx_transfer_count%8==0?8:rx_transfer_count%8,
                DL_I2C_CONTROLLER_START_DISABLE,
                DL_I2C_CONTROLLER_STOP_ENABLE,
                DL_I2C_CONTROLLER_ACK_DISABLE
            );
        }
        /*如果只有一组数据*/
        else if((rx_transfer_count-1)/8+1==1)
        {
            rx_buffer_group_size_verify=rx_transfer_count;
            DL_I2C_startControllerTransferAdvanced(this_i2c, slave_addr,DL_I2C_CONTROLLER_DIRECTION_RX,rx_transfer_count,
                DL_I2C_CONTROLLER_START_ENABLE,
                DL_I2C_CONTROLLER_STOP_ENABLE,
                DL_I2C_CONTROLLER_ACK_DISABLE
            );
        }

        i2c_wait_controller_status(this_i2c,DL_I2C_CONTROLLER_STATUS_ERROR,true,UINT16_MAX);
        
        if(is_i2c_dma_enabled==false)
        {
            /*检测实际接收字节数与预定接收字节数是否相等*/
            for(uint16_t j=0;j<1000;j++)
            {
                delay_cycles(80);//delay 1us
                rx_buffer_group_size=DL_I2C_getControllerRXFIFOCounter(this_i2c);
                if((rx_buffer_group_size_verify!=rx_buffer_group_size)&&j==1000-1)
                {
                    this_i2c_status=I2C_FAILED;
                    break;
                }
                else if(rx_buffer_group_size_verify==rx_buffer_group_size)
                {
                    break;
                }
            }

            /*将RXFIFO复制到rx_buffer*/
            for(uint8_t j=0;j<rx_buffer_group_size;j++)
            {
                rx_buffer[received_count++] = DL_I2C_receiveControllerData(this_i2c);     
            }       
        }
        else
        {
            /*检测实际接收字节数与预定接收字节数是否相等*/
            for(uint16_t j=0;j<1000;j++)
            {
                delay_cycles(80);//delay 1 us
                rx_buffer_group_size=DL_I2C_getControllerRXFIFOCounter(this_i2c);
                if((rx_buffer_group_size_verify!=rx_buffer_group_size)&&j==1000-1)
                {
                    this_i2c_status=I2C_FAILED;
                    break;
                }
                else if(rx_buffer_group_size_verify==rx_buffer_group_size)
                {
                    break;
                }
            }
            /*将RXFIFO复制到rx_buffer*/ 
            DL_DMA_setTransferSize(DMA,DMA_I2C_READ_CHAN_ID,rx_buffer_group_size);
            DL_DMA_startTransfer(DMA,DMA_I2C_READ_CHAN_ID);

            if(DL_I2C_getControllerRXFIFOCounter(this_i2c)>=1)
            {
                if(i2c_wait_controller_status(this_i2c,I2C_RXFIFO_NOT_EMPTY,true,20)==false)
                {
                    this_i2c_status=I2C_FAILED;
                }
            }
        }
    }
    
    /*等待I2C通信完毕。如果等待超时，重置i2c通信*/
    if(i2c_wait_controller_status(this_i2c,DL_I2C_CONTROLLER_STATUS_ERROR,true,200)==false)
    {
        DL_I2C_resetControllerTransfer(this_i2c);//重置i2c通信
        this_i2c_status=I2C_ERROR;
    }
    /*等待I2C和I2C总线空闲*/
    i2c_wait_controller_status(this_i2c,DL_I2C_CONTROLLER_STATUS_IDLE,false,UINT16_MAX);
    i2c_wait_controller_status(this_i2c,DL_I2C_CONTROLLER_STATUS_BUSY_BUS,true,UINT16_MAX);
    
    /*复制读取的数据，返回状态*/
    if(this_i2c_status==I2C_OK)memcpy(data,rx_buffer,rx_transfer_count);
    i2c_status[this_i2c_serial_num] = this_i2c_status;
    return i2c_status[this_i2c_serial_num];
}

/**
 * @brief i2c写入单个数据
 * @param this_i2c i2c句柄
 * @param slave_addr 从机地址
 * @param reg_addr 寄存器地址
 * @param data 写入数据
 * @return i2c写入后状态
 **/
int8_t i2c_write_one(I2C_Regs *this_i2c,uint8_t slave_addr,uint8_t reg_addr,uint8_t data)
{
    return i2c_write(this_i2c,slave_addr,(uint16_t)reg_addr,&data,1);
}

/**
 * @brief i2c读取单个数据
 * @param this_i2c i2c句柄
 * @param slave_addr 从机地址
 * @param reg_addr 寄存器地址
 * @param data 读取的单个数据指针
 * @return i2c读取后状态
 **/
int8_t i2c_read_one(I2C_Regs *this_i2c,uint8_t slave_addr,uint8_t reg_addr,uint8_t* data)
{
    int8_t this_i2c_status=I2C_OK;
    uint8_t data_single=0x00;

    this_i2c_status=i2c_read(this_i2c,slave_addr,(uint16_t)reg_addr,&data_single,1);
    *data=data_single;
    
    return this_i2c_status;
}

/********************静态函数定义********************/

/**
 * @brief i2c等待某个状态
 * 
 * @param this_i2c i2c句柄
 * @param i2c_status_assert 所等待的状态
 * @param invert 实际等待的状态是否为它的反转状态
 * @param tick_to_wait 最长等待时间
 * @return true 所等待的状态/反转状态已经到来
 * @return false 等待超时
 */
static bool i2c_wait_controller_status(I2C_Regs *this_i2c,uint32_t i2c_status_assert,bool invert, uint16_t tick_to_wait)
{
    bool status=false;

    /*限制等待时间为40ms*/
    for(uint32_t tick=0;tick<tick_to_wait&&tick<40;tick++)
    {
        if((DL_I2C_getControllerStatus(this_i2c) &i2c_status_assert)^invert)
        {
            status=true;
            break;
        }
        else if(i2c_status_assert==I2C_RXFIFO_NOT_EMPTY)
        {
            if((DL_I2C_isControllerRXFIFOEmpty(this_i2c)^invert)==false) 
            {
                status=true;
                break;
            }
            else
            {
                goto delay;
            }
        }
        else if(i2c_status_assert==I2C_TXFIFO_IS_EMPTY)
        {
            if((DL_I2C_isControllerTXFIFOEmpty(this_i2c)^invert)==true)
            {
                status=true;
                break;
            }
            else
            {
                goto delay;
            }        
        }
        else
        {
            delay:
            status=false; 
            delay_ms(1); 
        }
    }

    return status;
}

/**
 * @brief 查找i2c句柄
 * 
 * @param this_i2c 要查找的i2c句柄
 * @return uint8_t i2c句柄号，如果句柄号大于等于 @ref I2C_NUM 即表示该句柄不存在
 */
static uint8_t which_i2c_inst(I2C_Regs *this_i2c)
{
    uint8_t i2c_number=I2C_NUM;
    if(this_i2c==I2C0)
    {
        i2c_number=0;
    }
    else if(this_i2c==I2C1)
    {
        i2c_number=1;
    }

    return i2c_number;
}

    /*I2C发送至寄存器*/
    /*逐个发送(测试用)*/
    // for (uint16_t i = 0; i < tx_transfer_count; i++)
    // {
    //     DL_I2C_flushControllerTXFIFO(this_i2c);//清空TXFIFO
    //     DL_I2C_fillControllerTXFIFO(this_i2c,tx_buffer+i,1);//TXFIFO填充发送内容

    //     if(tx_transfer_count==1)
    //     {
    //         DL_I2C_startControllerTransferAdvanced(this_i2c, slave_addr,DL_I2C_CONTROLLER_DIRECTION_TX,
    //             1,
    //             DL_I2C_CONTROLLER_START_ENABLE,
    //             DL_I2C_CONTROLLER_STOP_ENABLE,
    //             DL_I2C_CONTROLLER_ACK_ENABLE     
    //         );   
    //     }
    //     else if(i==0)
    //     {
    //         DL_I2C_startControllerTransferAdvanced(this_i2c, slave_addr,DL_I2C_CONTROLLER_DIRECTION_TX,
    //             1,
    //             DL_I2C_CONTROLLER_START_ENABLE,
    //             DL_I2C_CONTROLLER_STOP_DISABLE,
    //             DL_I2C_CONTROLLER_ACK_ENABLE     
    //         );   
    //     }
    //     else if(i==tx_transfer_count-1)
    //     {
    //         DL_I2C_startControllerTransferAdvanced(this_i2c, slave_addr,DL_I2C_CONTROLLER_DIRECTION_TX,
    //             1,
    //             DL_I2C_CONTROLLER_START_DISABLE,
    //             DL_I2C_CONTROLLER_STOP_ENABLE,
    //             DL_I2C_CONTROLLER_ACK_ENABLE     
    //         );   
    //     }
    //     else if(i!=0)
    //     {
    //         DL_I2C_startControllerTransferAdvanced(this_i2c, slave_addr,DL_I2C_CONTROLLER_DIRECTION_TX,
    //             1,
    //             DL_I2C_CONTROLLER_START_DISABLE,
    //             DL_I2C_CONTROLLER_STOP_DISABLE,
    //             DL_I2C_CONTROLLER_ACK_ENABLE     
    //         );   
    //     }
    //     i2c_wait_controller_status(this_i2c,I2C_TXFIFO_IS_EMPTY,UINT16_MAX);
    // }

    /*逐个接收(测试用)*/
    // /*读取寄存器内容*/
    // for(uint16_t i=0;i<rx_transfer_count&&i<UINT16_MAX;i++)
    // {
    //     /*如果读取第一个字节*/
    //     if((i==0)&&(i!=rx_transfer_count-1||i==UINT16_MAX-1)&&rx_transfer_count!=1)
    //     {
    //         DL_I2C_startControllerTransferAdvanced(this_i2c, slave_addr,DL_I2C_CONTROLLER_DIRECTION_RX,
    //             rx_transfer_count-i>8?8:rx_transfer_count-i-1,  //多减去的1个读取字节用来执行“如果读取最后一个字节”情况以发送iic结束信号
    //             DL_I2C_CONTROLLER_START_ENABLE,
    //             DL_I2C_CONTROLLER_STOP_DISABLE,
    //             DL_I2C_CONTROLLER_ACK_ENABLE
    //         );
    //     }
    //     /*受MSPM0 FIFO限制，读取超过8以上的字节时，每8个字节需要再发起一次接收,隐含条件：i!=0*/
    //     else if((i%8==0)&&!(i==rx_transfer_count-1||i==UINT16_MAX-1)&&rx_transfer_count!=1)
    //     {
    //         DL_I2C_startControllerTransferAdvanced(this_i2c, slave_addr,DL_I2C_CONTROLLER_DIRECTION_RX,
    //             rx_transfer_count-i>8?8:rx_transfer_count-i-1,
    //             DL_I2C_CONTROLLER_START_DISABLE,
    //             DL_I2C_CONTROLLER_STOP_DISABLE,
    //             DL_I2C_CONTROLLER_ACK_ENABLE
    //         );
    //     }
    //     /*如果读取最后一个字节*/
    //     else if((i==rx_transfer_count-1||i==UINT16_MAX-1)&&rx_transfer_count!=1)
    //     {
    //         DL_I2C_startControllerTransferAdvanced(this_i2c, slave_addr,DL_I2C_CONTROLLER_DIRECTION_RX,1,
    //             DL_I2C_CONTROLLER_START_DISABLE,
    //             DL_I2C_CONTROLLER_STOP_ENABLE,
    //             DL_I2C_CONTROLLER_ACK_DISABLE
    //         );
    //     }
    //     /*如果只读取一个字节*/
    //     else if(rx_transfer_count==1)
    //     {
    //         DL_I2C_startControllerTransferAdvanced(this_i2c, slave_addr,DL_I2C_CONTROLLER_DIRECTION_RX,1,
    //             DL_I2C_CONTROLLER_START_ENABLE,
    //             DL_I2C_CONTROLLER_STOP_ENABLE,
    //             DL_I2C_CONTROLLER_ACK_DISABLE
    //         );
    //     }

    //     i2c_wait_controller_status(this_i2c,DL_I2C_CONTROLLER_STATUS_ERROR,true,UINT16_MAX);
    //     /*等待 读取字节 到 RXFIFO*/
    //     if(i2c_wait_controller_status(this_i2c,I2C_RXFIFO_NOT_EMPTY,false,30)==true)
    //     {
    //         rx_buffer[i] = DL_I2C_receiveControllerData(this_i2c);
    //     }
    //     else
    //     {
    //       this_i2c_status=I2C_FAILED;
    //     }
    // }